package com.bootdo.contract.app;

import com.alibaba.fastjson.JSONObject;
import com.bootdo.common.controller.BaseController;
import com.bootdo.common.utils.R;
import com.bootdo.contract.domain.AlarmDO;
import com.bootdo.contract.domain.MissionDO;
import com.bootdo.contract.domain.SiteDO;
import com.bootdo.contract.service.AlarmService;
import com.bootdo.contract.service.MissionService;
import com.bootdo.contract.service.SiteService;
import com.bootdo.system.domain.DeptDO;
import com.bootdo.system.domain.UserDO;
import com.bootdo.system.service.DeptService;
import com.bootdo.system.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import static com.bootdo.contract.utils.Map.isInCircle;


/*
 * app 公共访问方式
 */
@RestController
@RequestMapping("/app/online")
public class OnlineController extends BaseController {
	private Logger logger = LoggerFactory.getLogger(OnlineController.class);

	@Autowired
	private MissionService missionService;

	@Autowired
	private SiteService siteService;
	@Autowired
	private UserService userService;
	@Autowired
	private DeptService sysDeptService;
	@Autowired
	private AlarmService alarmService;

	/**
	 * 根据用户id获取任务列表
	 * @param json
	 * @return
	 */
	@ResponseBody
	@PostMapping("/missionList")
	public R missionList(@RequestBody String json){
		Map map = JSONObject.parseObject(json, Map.class);
		String userId = (String) map.get("userId");
		String status = (String) map.get("status");

		MissionDO missionDOa = new MissionDO();
		missionDOa.setUserId(Long.valueOf(userId));
		missionDOa.setStatus(Long.valueOf(status));

		List<MissionDO> missionDOs = missionService.getMissionListByUserId(missionDOa);
		return R.ok().put("data",missionDOs);
	}

	/**
	 * 修改任务
	 * @param json
	 * @return
	 */
	@ResponseBody
	@PostMapping("/addMissionDO")
	public R addMissionDO(@RequestBody String json){
		Map map = JSONObject.parseObject(json, Map.class);
		String id = (String) map.get("id");
		String content = (String) map.get("content");
		String picid1 = (String) map.get("picid1");
		String picid2 = (String) map.get("picid2");
		String picid3 = (String) map.get("picid3");

		MissionDO missionDO = new MissionDO();
		if(picid1!=null && picid1 != ""){
			missionDO.setPicid1(Long.valueOf(picid1));
		}
		if(picid2!=null && picid2 != ""){
			missionDO.setPicid2(Long.valueOf(picid2));
		}
		if(picid3!=null && picid3 != ""){
			missionDO.setPicid3(Long.valueOf(picid3));
		}
		if(content!=null && content != ""){
			missionDO.setContent(content);
		}
		missionDO.setId(Long.valueOf(id));
		missionService.update(missionDO);
		return R.ok();
	}

	/**
	 * 获取经纬度
	 * @param json
	 * @return
	 */
	@ResponseBody
	@PostMapping("/addSite")
	public R addSite(@RequestBody String json) throws ParseException {
		Map map = JSONObject.parseObject(json, Map.class);
		String userId = (String) map.get("userId");
		String latitude = (String) map.get("latitude");
		String longitude = (String) map.get("longitude");
		String gmtCreate = (String) map.get("gmtCreate");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		SiteDO siteDO = new SiteDO();
		siteDO.setUserid(Long.valueOf(userId));
		siteDO.setLatitude(latitude);
		siteDO.setLongitude(longitude);
		siteDO.setGmtCreate(sdf.parse(gmtCreate));
		siteService.save(siteDO);

		/*根据id获取部门*/
		UserDO user = userService.get(Long.valueOf(userId));
		DeptDO deptDO =sysDeptService.get(user.getDeptId());

		double x = Double.parseDouble(deptDO.getJing());//圆心x坐标
		double y =  Double.parseDouble(deptDO.getWei());//圆心y坐标
		double r = Double.parseDouble(deptDO.getZhong());//圆的半径
		double x1 = Double.parseDouble(longitude);//点的x坐标
		double y1 =  Double.parseDouble(latitude);//点得y坐标


		if(isInCircle(r,x,y,x1,y1)){//判断
			System.out.println("在圆内");
		}else {
			System.out.println("超出了围栏范围");
			AlarmDO alarmDO =new AlarmDO();
			alarmDO.setGmtCreate(new Date());
			alarmDO.setUserName(user.getName());
			alarmDO.setAlarm("超出了围栏范围");
			alarmService.save(alarmDO);
		}

		return R.ok();
	}

}
